package net.wsfcp.android.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import net.wsfcp.android.R;
import net.wsfcp.android.bean.resultbean.OrderBean;
import net.wsfcp.android.ui.widget.RecycleViewDivider;
import net.wsfcp.android.utils.DpUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

//订单列表
public class OrderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "MultipleAdapter";
    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<OrderBean.OrderList> orderBean = new ArrayList<>();

    public ArrayList<OrderBean.OrderList> getOrderBean() {
        return orderBean;
    }

    public void setOrderBean(ArrayList<OrderBean.OrderList> orderBean) {
        this.orderBean = orderBean;
        notifyDataSetChanged();
    }

    public OrderAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
//        this.orderBean = orderBean;
    }


    OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        public void onItemClick(View view, int position);

        public void onItemLongClick(View view, int position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_list_order, parent, false);
        return new NormalItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        NormalItemViewHolder viewHolder = (NormalItemViewHolder) holder;

        OrderBean.OrderList bean = orderBean.get(position);
        viewHolder.txt_message.setText(bean.getMessage());
        viewHolder.txt_order_num.setText(mContext.getResources().getString(R.string.order_list_order_code) + bean.getOrderNumber());
        viewHolder.txt_show_status.setText(bean.getShowStatus());
        viewHolder.txt_create_time.setText("下单时间:" + bean.getShowCreateTime());
        viewHolder.txt_num.setText(bean.getBuyNum() + "");
        viewHolder.txt_total.setText("￥" + bean.getPayAmount());
        viewHolder.txt_bill_number.setText("运单编号:" + bean.getWaybillNumber());

        if (bean.getStatus() == 1) {
            viewHolder.txt_confirm.setVisibility(View.VISIBLE);
        } else {
            viewHolder.txt_confirm.setVisibility(View.GONE);
        }

        if (bean.isCanCancel()) {
            viewHolder.txt_cancel.setVisibility(View.VISIBLE);
        } else {
            viewHolder.txt_cancel.setVisibility(View.GONE);
        }


        if (onItemClickListener != null) {
            viewHolder.txt_detail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(v, position);
                }
            });
            viewHolder.txt_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(v, position);
                }
            });
            viewHolder.txt_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(v, position);
                }
            });

        }


        RecyclerView recyclerView = viewHolder.rv_list;
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        if (recyclerView.getItemDecorationCount() == 0) {
            recyclerView.addItemDecoration(new RecycleViewDivider(mContext, DpUtils.dp2px(8), DpUtils.dp2px(8), true));
        }
        ArrayList<OrderBean.OrderList.Order2GoodsList> list = orderBean.get(position).getOrder2GoodsList();
        OrderListAdapter adapter = new OrderListAdapter(list, mContext);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public int getItemCount() {
        return orderBean.size();
    }


    public class NormalItemViewHolder extends RecyclerView.ViewHolder {
        TextView txt_detail;

        TextView txt_message;
        TextView txt_order_num;
        TextView txt_show_status;
        TextView txt_create_time;
        TextView txt_num;
        TextView txt_total;
        TextView txt_bill_number;
        TextView txt_cancel;
        TextView txt_confirm;


        RecyclerView rv_list;

        public NormalItemViewHolder(View itemView) {
            super(itemView);
            txt_message = itemView.findViewById(R.id.txt_message);
            txt_order_num = itemView.findViewById(R.id.txt_order_num);
            txt_show_status = itemView.findViewById(R.id.txt_show_status);
            txt_create_time = itemView.findViewById(R.id.txt_create_time);
            txt_num = itemView.findViewById(R.id.txt_num);
            txt_total = itemView.findViewById(R.id.txt_total);
            txt_bill_number = itemView.findViewById(R.id.txt_bill_number);
            txt_detail = itemView.findViewById(R.id.txt_detail);
            txt_cancel = itemView.findViewById(R.id.txt_cancel);
            txt_confirm = itemView.findViewById(R.id.txt_confirm);

            rv_list = itemView.findViewById(R.id.rv_list);
        }
    }


}





